
#include <stdio.h>
#include <malloc.h>
#include <execinfo.h>
#include <iostream>
#include <vector>
#include <pthread.h>
#include <unistd.h>
#include <sys/prctl.h>
#include <sys/syscall.h>
#include "mmd/hzmm_nwm.h"

typedef struct student {
    int data[100];
}STU;

void _function1_()
{
    int * ptr = NULL;
    ptr = (int *)malloc(10);
    free(ptr);
}

void _function2_()
{
    std::vector<int> d(100);
    d.clear();
}

void _function3_()
{
    std::vector<STU> d(1);
    STU d1;
    d.push_back(d1);
    d.clear();
}

void _function4_()
{
    STU * s = new(STU);
    free(s);
}


void _function5_()
{
    int *ptr = (int*)malloc(10);
    ptr = (int*)realloc(ptr, 20);
    free(ptr);
    ptr = (int*)memalign(64, 10);
    free(ptr);
    ptr = (int *)calloc(sizeof(int), 100);
    free(ptr);
}

static void *pthread_1(void *)
{
    prctl(PR_SET_NAME, "thread_1", 0,0,0);
    int i = 0;
    for (i = 0; i < 2000000; i++) {
        int *ptr = (int *) malloc(10 * sizeof(int));
        free(ptr);
        int j = 0;
        for (j = 0; j < 10000; j++);
        //sleep(1);
    }
    return NULL;
}

static void *pthread_2(void *)
{
    prctl(PR_SET_NAME, "thread_2", 0,0,0);
    int i = 0;
    for (i = 0; i < 2000000; i++) {
        int *ptr = new(int);
        free(ptr);
        //_function4_();
        int j = 0;
        for (j = 0; j < 10000; j++);
        //sleep(1);
    }
    return NULL;

}

int main()
{
    //init_nwm_caller();

    //_function1_();
    //_function2_();
    //_function3_();
    //_function4_();
    //_function5_();
    //int *ptr = new int[10];
    //delete ptr;
    //_func1_1();

    pthread_t pt_1;
    pthread_t pt_2;
    pthread_attr_t attr;
    pthread_attr_init(&attr);
    pthread_create(&pt_1, NULL, pthread_1, NULL);
    pthread_create(&pt_2, NULL, pthread_2, NULL);

    update_name();

    pthread_join(pt_1, NULL);
    pthread_join(pt_2, NULL);
    return 0;
}


#if 0
typedef struct mydata {
    int d[10240];//40KB
    //int d[2097152];//8MB
} DATA;

void print_ptr(std::vector<DATA> & d)
{
    std::cout << "---- " << d.capacity() << std::endl;
    for (int i = 0; i < d.size(); i++)
    {
        std::cout << &d[i] << std::endl;
    }
    std::cout << std::endl;
}

int main()
{
    DATA d;
    std::vector<DATA> data(5);
    print_ptr(data);
    //data.pop_back();
    //print_ptr(data);
    //data.erase(data.begin()+1, data.begin()+2);
    //print_ptr(data);
    //data.push_back(d);
    //data.push_back(d);
    //data.push_back(d);
    //print_ptr(data);
    
    std::string info;
    while (1)
    {
        std::cout << "Param: push_back pop_back erase_mid erase_start" << std::endl;
        std::cin >> info;
        if ("q" == info) {
            break;
        }
        else if ("push_back" == info) {
            data.push_back(d);
        } 
        else if ("pop_back" == info) {
            data.pop_back();
        }
        else if ("erase_mid" == info) {
            data.erase(data.begin()+1, data.begin()+2);
        }
        else if ("erase_start" == info) {
            data.erase(data.begin(), data.begin()+1);
        }
        else {
            std::cout << "param error!" << std::endl;
            continue;
        }
        print_ptr(data);
    }

    return 0;
}
#endif